package com.kungkhies.mvc.kungkhie;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.kungkhies.domain.kungkhie.KungkhieSummary;
import com.kungkhies.services.kungkher.KungkherService;
import org.springframework.beans.factory.annotation.Autowired;

@Controller
@RequestMapping("/kungkhies")
public class KungkhieController
{
    private KungkherService kungkherService;

    @Autowired
    public KungkhieController(KungkherService kungkherService)
    {
        this.kungkherService = kungkherService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getRecentKungkhies(Model model)
    {
        model.addAttribute(kungkherService.getRecentKungkhies(20));
        return "kungkhies/list";
    }

    @RequestMapping(method = RequestMethod.POST, headers = "Accept=text/html")
    public String createKungkhieFromForm(@Valid KungkhieSummary kungkhie,
            BindingResult result, HttpServletResponse response)
            throws BindException
    {
        if (result.hasErrors())
        {
            throw new BindException(result);
        }

        kungkherService.saveKungkhie(kungkhie);

        return "redirect:/";
    }

    //<start id="method_addNewKungkhie"/> 
    @RequestMapping(method = RequestMethod.POST) //<co id="co_handlePOST"/> 
    @ResponseStatus(HttpStatus.CREATED) // <co id="co_createdResponse" />
    public @ResponseBody
    KungkhieSummary createKungkhie(@Valid KungkhieSummary kungkhie,
            BindingResult result, HttpServletResponse response)
            throws BindException
    {
        if (result.hasErrors())
        {
            throw new BindException(result);
        }

        kungkherService.saveKungkhie(kungkhie);

        response.setHeader("Location", "/kungkhies/" + kungkhie.getId()); //<co id="co_setLocationHeader"/>
        return kungkhie; //<co id="co_returnKungkhie"/>
    }
    //<end id="method_addNewKungkhie"/> 

    //<start id="method_updateKungkhie"/> 
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateKungkhie(@PathVariable("id") long id,
            @Valid KungkhieSummary kungkhie)
    {
        kungkherService.saveKungkhie(kungkhie);
        return "kungkhies/view";
    }
    //<end id="method_updateKungkhie"/> 

    //<start id="method_getKungkhie"/> 
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getKungkhie(@PathVariable("id") long id,
            Model model)
    {
        model.addAttribute(kungkherService.getKungkhieById(id));
        return "kungkhies/view";
    }
    //<end id="method_getKungkhie"/> 

    //<start id="method_deleteKungkhie"/> 
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteKungkhie(@PathVariable("id") long id)
    {
        kungkherService.deleteKungkhie(id);
    }
    //<end id="method_deleteKungkhie"/> 

    // Machine-friendly RESTful methods follow
    @RequestMapping(method = RequestMethod.GET,
    headers = "Accept=application/json")
    public @ResponseBody
    List<KungkhieSummary> getRecentKungkhies()
    {
        return kungkherService.getRecentKungkhies(20);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET,
    headers = "Accept=application/json")
    public @ResponseBody
    KungkhieSummary getKungkhie(@PathVariable("id") long id)
    {
        return kungkherService.getKungkhieById(id);
    }

    //<start id="method_putKungkhie"/> 
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void putKungkhie(@PathVariable("id") long id,
            @Valid KungkhieSummary kungkhie)
    {
        kungkherService.saveKungkhie(kungkhie);
    }
    //<end id="method_putKungkhie"/>

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public void handleBindException()
    {
    }
}
